<?php

function list_hari_libur(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');
	
	$header = array(
		'Tanggal '=>array('style'=>'text-align:left;border-bottom:2px solid;width:25%'),  
		'Keterangan '=>array('style'=>'text-align:left;border-bottom:2px solid;width:45%'),  
		'Hari Raya'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),  
		'Tiap Tahun'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	$query = "SELECT * FROM a_hari_libur ORDER BY hari_libur_id ASC ";
	$result = my_query($query);
	$i=0;
	$row = array();

	while($ey = my_fetch_array($result) ){
		$i++;
		$editproperty = array(
				'href'=>'index.php?com=a_hari_libur&task=edit&id='.$ey['hari_libur_id'] ,
				'rel'=>'facebox',
				'title'=>'Edit'
		);	
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:;',
			'title'=>'Delete',
			'onclick'=>'confirmDelete('.$ey['hari_libur_id'].')',
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );

		$row[] = array(
			'tgl'=>position_text_align ( date("d-m-Y", strtotime($ey['tanggal'] ." 00:00:00" ) ), 'left'), 
			'nama'=>  $ey['nama_hari_libur'] ,     
			'hari_raya'=>  $ey['hari_raya'] ,     
			'repeat_year '=>  $ey['repeat_year'] ,     
			'operasi'=> position_text_align( $edit_button .' '.$delete_button , 'right'),
		);
	}
	$datas = table_rows($row);
	$prosedur_tambah='javascript: jQuery.facebox({ ajax: \'index.php?com=a_hari_libur&task=edit&id=0\' });' ;
	$prosedur_tahun_berikut='javascript: jQuery.facebox({ ajax: \'index.php?com=a_hari_libur&task=create\' });' ;

	$menuontop = array('Tambah' => array('onclick'=>$prosedur_tambah),
		//'Tahun Berikut'=>array('onclick'=>'javascript:location.href=\'index.php?com=a_hari_libur&task=create\''),
		);
 	$printmenu = additional_menu_on_list( $menuontop ,'');
	return $printmenu.table_builder($header , $datas ,  5 , false );;
}

function form_hari_libur_create($hari_libur_id){

	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);
	$view = form_header( "hari_libur" , "hari_libur"  );
	$fields = my_get_data_by_id('a_hari_libur','hari_libur_id',$hari_libur_id);

	$ftanggalm = $ftanggal = date('d-m-Y');
	if($fields){
		list($yyyytanggal , $mmtanggal,$ddtanggal ) = explode("-" ,$fields['tanggal'] );
		$ftanggal = $ddtanggal.'-'.$mmtanggal.'-'.$yyyytanggal;
	}
	
	$tanggal = array(
			'name'=>'tanggal',
			'value'=>(isset($_POST['tanggal'])? $_POST['tanggal'] : $ftanggal),
			'id'=>'tanggal',
			'type'=>'textfield',
			'size'=>'45'
		);
	$form_tanggal = form_calendar($tanggal);
	$view .= form_field_display( $form_tanggal  , "Tanggal" );
	
	$nama_hari_libur = array(
			'name'=>'nama_hari_libur',
			'value'=>(isset($_POST['nama_hari_libur'])? $_POST['nama_hari_libur'] : $fields['nama_hari_libur']),
			'id'=>'nama_hari_libur',
			'type'=>'textfield',
			'size'=>'35'
		);
	$form_nomor = form_dynamic($nama_hari_libur);
	$view .= form_field_display( $form_nomor  , "Keterangan"  );

	if($fields['hari_raya']=='Y'){ 
		$hari_raya  = array(
			'name'=>'hari_raya',
			'id'=>'hari_raya',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$hari_raya  = array(
			'name'=>'hari_raya',
			'id'=>'hari_raya',
			'type'=>'checkbox',
		); 
	}
	$form_hari_raya = form_dynamic($hari_raya ).'Hari raya';
	$view .= form_field_display( $form_hari_raya , "" );	
	
	if($fields['repeat_year']=='Y'){ 
		$repeat_year  = array(
			'name'=>'repeat_year',
			'id'=>'repeat_year',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$repeat_year  = array(
			'name'=>'repeat_year',
			'id'=>'repeat_year',
			'type'=>'checkbox',
		); 
	}
	$form_repeat_year = form_dynamic($repeat_year ).'Berulang setiap tahun';
	$view .= form_field_display( $form_repeat_year , "" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );	
	return $view;
}

function form_hari_libur_submit($hari_libur_id = 0){
	if($_POST){
		list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal'] );
		$ftanggal = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
	}
	$repeat_year = isset($_POST['repeat_year']) ? 'Y': 'T';
	$hari_raya = isset($_POST['hari_raya']) ? 'Y': 'T';
	$datas = array(
			'tanggal'=>my_type_data_str($ftanggal), 
			'nama_hari_libur'=>my_type_data_str($_POST['nama_hari_libur']), 	
			'hari_raya'	 =>my_type_data_str($hari_raya),		
			'repeat_year'	 =>my_type_data_str($repeat_year),		
		);
	if($hari_libur_id > 0){
		return my_update_record('a_hari_libur','hari_libur_id',$hari_libur_id, $datas);
	}
	return my_insert_record('a_hari_libur' , $datas);
}

function form_hari_libur_validate($hari_libur_id){
	$errsubmit = false;
	$err = array();

	$code =  trim($_POST["nama_hari_libur"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Keterangan belum di isi";
	}

	list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal'] );
	$tahun1=(int) $yyyytanggal;
	$temps=my_fetch_array(my_query("SELECT YEAR(tanggal) as tahun FROM a_hari_libur ORDER BY tanggal desc limit 1"));
	$tahun2=$temps['tahun'];
	if($tahun1 < $tahun2){
		$errsubmit = true;
		$err[] = "Tahun tidak boleh <".$tahun2 ;
	}
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_hari_libur($hari_libur_id){
	$query = "DELETE FROM a_hari_libur WHERE hari_libur_id ={$hari_libur_id} ";
	return my_query($query);
}	

function generate_next_year(){
	$query = "
		SELECT YEAR(tanggal) AS tahun FROM a_hari_libur 
		ORDER BY tanggal DESC LIMIT 1" ;
	$result = my_query($query);
	$data = my_fetch_array($result);
	$tahun=$data['tahun']; 

	$query = "
		SELECT * FROM a_hari_libur 
		WHERE repeat_year ='Y' AND 
		YEAR(tanggal)=".$tahun ;
	$result = my_query($query); 
	
	while($row = my_fetch_array($result) ){
		list($yyyytanggal , $mmtanggal,$ddtanggal ) = explode("-" ,$row['tanggal'] );	
		$tanggal = $yyyytanggal+1 .'-'.$mmtanggal.'-'.$ddtanggal;
		$datas = array(
			'tanggal'=>my_type_data_str($tanggal),
			'nama_hari_libur'=>my_type_data_str($row['nama_hari_libur']), 
			'hari_raya'=>my_type_data_str($row['hari_raya']), 
			'repeat_year'=>my_type_data_str($row['repeat_year']), 
		);	
		my_insert_record('a_hari_libur' , $datas);		
	}
	return true;
}